﻿using LpbPrj.Client.Data;
using LpbPrj.Client.Tools.Helper;
using Newtonsoft.Json;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;


namespace LpbPrj.Client.Models
{
    /// <summary>
    /// 三维坐标点，对焦提示，DLL结构体转类
    /// </summary>
    public class Point3dResult
    {
        public double? X { get; set; }
        public double? Y { get; set; }
        public double? Z { get; set; }

        public override string ToString()
        {
            return $"x:{X.Value.ToString("f2")},y:{Y.Value.ToString("f2")},z:{Z.Value.ToString("f2")}";
        }
    }

    /// <summary>
    /// 电机移动范围
    /// </summary>
    public class MotorPosResult
    {
        public int X1 { get; set; }
        public int X2 { get; set; }
        public int Y1 { get; set; }
        public int Y2 { get; set; }
        public int Z1 { get; set; }
        public int Z2 { get; set; }
        public int H1 { get; set; }
        public int H2 { get; set; }

        public double X { get; set; }
        public double Y { get; set; }
        public double Z { get; set; }
        public double H { get; set; }

        public double StepX { get; set; }
        public double StepY { get; set; }
        public double StepZ { get; set; }
        public double StepH { get; set; }

        public MotorPosResult Clone()
        {
            return (MotorPosResult)this.MemberwiseClone();
        }

    }

    /// <summary>
    /// 检验结果基类
    /// </summary>
    public class IolMasterResultBase
    {
        public DateTime DateTime { get; set; } = DateTime.Now;

        public virtual void Save(string examDataSavePath, string fileName) { }
    }

    /// <summary>
    /// 光A波形
    /// </summary>
    public class IolMasterALineResultSimple : IolMasterResultBase
    {
        public double? AL { get; set; }	///<眼轴,单位毫米,无效数据为NaN,通过double.IsNaN判断是否为NaN
		public double? CCT { get; set; }  ///<角膜,单位毫米,无效数据为NaN
		public double? AD { get; set; }	///<前房,单位毫米,无效数据为NaN
		public double? LT { get; set; }	///<晶体,单位毫米,无效数据为NaN
		public double? VT { get; set; }	///<玻璃体,单位毫米,无效数据为NaN
		public double? RT { get; set; } ///<视网膜,单位毫米,无效数据为NaN

    }
    public class IolMasterALineResult : IolMasterALineResultSimple
    {
        public int? pkPreCorneal;
        public int? pkPostCorneal;
        public int? pkPreLens;
        public int? pkPostLens;
        public int? pkRetina;

        public int lineLengh;
        [JsonIgnore]
        public List<short> lineData = new List<short>();

        public double? CoeffA = null;
        public double? CoeffB = null;

        public double VitSpeed = 1.3484;
        public double VitSpeedLenNon;
        public double VitSiliconeOilSpeedLenNon = 1.3484;

        public int? LensType = null;

        [JsonIgnore]
        protected System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(100, 0, 255, 0), 1);

        public override void Save(string examDataSavePath, string fileName)
        {
            int width = 1000;
            int height = 300;

            if (lineData != null && lineData.Count > 0)
            {
                float scaleX = (float)(width * 1.0 / lineData.Count);
                float scaleY = (float)(height * 1.0 / 3072);

                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                {
                    using (System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bmp))
                    {
                        //设置高质量插值法
                        gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        //设置高质量,低速度呈现平滑程度
                        gThumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        //设置高质量合成图像
                        gThumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                        //填充背景
                        gThumb.FillRectangle(System.Drawing.Brushes.Gray, 0, 0, bmp.Width, bmp.Height);

                        //画线
                        for (int i = 0; i < lineData.Count - 1; i++)
                        {
                            gThumb.DrawLine(pen, i * scaleX, height - lineData[i] * scaleY, (i + 1) * scaleX, height - lineData[i + 1] * scaleY);
                        }
                    }
                    bmp.Save(System.IO.Path.Combine(examDataSavePath, $"AScan_{fileName}.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
                }
            }
            File.WriteAllText(System.IO.Path.Combine(examDataSavePath, $"AScan_{fileName}.line"), JsonHelper.ToJson(lineData));
            File.WriteAllText(System.IO.Path.Combine(examDataSavePath, $"AScan_{fileName}.data"), JsonHelper.ToJson(this));


        }

    }

    /// <summary>
    /// 曲率
    /// </summary>
    public class IolMasterKeraResultSimple : IolMasterResultBase
    {
        public double? FlatR { get; set; } = null;
        public double? FlatK { get; set; } = null;
        public double? FlatA { get; set; } = null;
        public double? SteepR { get; set; } = null;
        public double? SteepK { get; set; } = null;
        public double? SteepA { get; set; } = null;
        public double? AST { get; set; } = null;
        public double? ASTA { get; set; } = null;
        public double? Pupil { get; set; } = null;

    }
    public class IolMasterKeraResult : IolMasterKeraResultSimple
    {
        public int ImageWidth = 1920;
        public int ImageHeight = 1536;
        [JsonIgnore]
        public List<byte> BitmapBytes = new List<byte>();

        public double? CenterX;
        public double? CenterY;
        public double? Diameter;
        public double? PixelSpacing;

        [JsonIgnore]
        protected System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(100, 0, 255, 0), 4);


        public override void Save(string examDataSavePath, string fileName)
        {
            using (System.Drawing.Bitmap bmpSrc = ImageShareHelper.Bytes2Bitmap(BitmapBytes.ToArray(), ImageWidth, ImageHeight, GlobalData.BmpGay8Palette))
            {
                bmpSrc.Save(System.IO.Path.Combine(examDataSavePath, $"Kera_{fileName}.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);

                //带有瞳孔直径的jpg图
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bmpSrc.Width, bmpSrc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                {
                    using (System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bmp))
                    {
                        //设置高质量插值法
                        gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        //设置高质量,低速度呈现平滑程度
                        gThumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        //设置高质量合成图像
                        gThumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                        //画原来的图像
                        gThumb.DrawImage(bmpSrc, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmpSrc.Width, bmpSrc.Height, System.Drawing.GraphicsUnit.Pixel);

                        //画圆
                        if (this.CenterX.HasValue && this.CenterY.HasValue && this.Diameter.HasValue && this.PixelSpacing.HasValue && this.CenterX.Value > 0 && this.CenterY.Value > 0 && this.Diameter.Value > 0)
                        {
                            gThumb.DrawEllipse(pen, new System.Drawing.Rectangle((int)(this.CenterX.Value - this.Diameter.Value * 0.5), (int)(this.CenterY.Value - this.Diameter.Value * 0.5), (int)this.Diameter.Value, (int)this.Diameter.Value));
                        }
                    }
                    bmp.Save(System.IO.Path.Combine(examDataSavePath, $"Kera_{fileName}.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

                }
            }

            File.WriteAllText(System.IO.Path.Combine(examDataSavePath, $"Kera_{fileName}.data"), JsonHelper.ToJson(this));
        }
    }

    /// <summary>
    /// 白对白
    /// </summary>
    public class IolMasterWTWResultSimple : IolMasterResultBase
    {
        public double? WTW { get; set; }//单位mm
    }
    public class IolMasterWTWResult : IolMasterWTWResultSimple
    {
        public int ImageWidth = 1920;
        public int ImageHeight = 1536;
        [JsonIgnore]
        public List<byte> BitmapBytes = new List<byte>();

        public double? CenterX;
        public double? CenterY;
        public double? Diameter;
        public double? PixelSpacing;

        [JsonIgnore]
        protected System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(100, 0, 255, 0), 4);

        public override void Save(string examDataSavePath, string fileName)
        {
            using (System.Drawing.Bitmap bmpSrc = ImageShareHelper.Bytes2Bitmap(BitmapBytes.ToArray(), ImageWidth, ImageHeight, GlobalData.BmpGay8Palette))
            {
                bmpSrc.Save(System.IO.Path.Combine(examDataSavePath, $"WTW_{fileName}.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);

                //带有瞳孔直径的jpg图
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bmpSrc.Width, bmpSrc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                {
                    using (System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bmp))
                    {
                        //设置高质量插值法
                        gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        //设置高质量,低速度呈现平滑程度
                        gThumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        //设置高质量合成图像
                        gThumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                        //画原来的图像
                        gThumb.DrawImage(bmpSrc, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmpSrc.Width, bmpSrc.Height, System.Drawing.GraphicsUnit.Pixel);

                        //画圆
                        if (this.CenterX.HasValue && this.CenterY.HasValue && this.Diameter.HasValue && this.PixelSpacing.HasValue && this.CenterX.Value > 0 && this.CenterY.Value > 0 && this.Diameter.Value > 0)
                        {
                            gThumb.DrawEllipse(pen, new System.Drawing.Rectangle((int)(this.CenterX.Value - this.Diameter.Value * 0.5), (int)(this.CenterY.Value - this.Diameter.Value * 0.5), (int)this.Diameter.Value, (int)this.Diameter.Value));
                        }
                    }
                    bmp.Save(System.IO.Path.Combine(examDataSavePath, $"WTW_{fileName}.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

                }
            }

            File.WriteAllText(System.IO.Path.Combine(examDataSavePath, $"WTW_{fileName}.data"), JsonHelper.ToJson(this));
        }

    }

    /// <summary>
    /// 验光
    /// </summary>
    public class IolMasterRefResultSimple : IolMasterResultBase
    {
        public double? SPH { get; set; }     //球镜度,单位D,无效数据为NaN
        public double? CYL { get; set; }   //柱镜度,单位D,无效数据为NaN
        public double? AXS { get; set; }       //轴位角,单位°,无效数据为NaN
        public double? Vertex { get; set; }     //距角膜顶点,单位毫米,无效数据为NaN
        public int Score { get; set; } = 0;
    }
    public class IolMasterRefResult : IolMasterRefResultSimple
    {
        public int ImageWidth = 640;
        public int ImageHeight = 512;

        [JsonIgnore]
        public List<byte> BitmapBytes = new List<byte>();
        public List<double> Keypoints_xList = new List<double>();
        public List<double> Keypoints_yList = new List<double>();

        [JsonIgnore]
        protected System.Drawing.Brush brush = new SolidBrush(System.Drawing.Color.FromArgb(255, 0, 255, 0));//画特征点的颜色
        [JsonIgnore]
        protected double keypointRadio = 4;//特征点的半径

        public override void Save(string examDataSavePath, string fileName)
        {
            using (System.Drawing.Bitmap bmpSrc = ImageShareHelper.Bytes2Bitmap(BitmapBytes.ToArray(), ImageWidth, ImageHeight, GlobalData.BmpGay8Palette))
            {
                bmpSrc.Save(System.IO.Path.Combine(examDataSavePath, $"Ref_{fileName}.bmp"), System.Drawing.Imaging.ImageFormat.Bmp);

                //带有瞳孔直径的jpg图
                using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(bmpSrc.Width, bmpSrc.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                {
                    using (System.Drawing.Graphics gThumb = System.Drawing.Graphics.FromImage(bmp))
                    {
                        //设置高质量插值法
                        gThumb.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                        //设置高质量,低速度呈现平滑程度
                        gThumb.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        //设置高质量合成图像
                        gThumb.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

                        //画原来的图像
                        gThumb.DrawImage(bmpSrc, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), 0, 0, bmpSrc.Width, bmpSrc.Height, System.Drawing.GraphicsUnit.Pixel);

                        //画特征点
                        if ((Keypoints_xList?.Count() == 360) && (Keypoints_yList?.Count() == 360))
                        {
                            for (int i = 0; i < 360; i += 10)
                            {
                                if (!Keypoints_xList[i].IsNaN() && !Keypoints_yList[i].IsNaN() && Keypoints_xList[i] > 0 && Keypoints_yList[i] > 0)
                                {
                                    gThumb.FillEllipse(brush, (float)(Keypoints_xList[i] - keypointRadio), (float)(Keypoints_yList[i] - keypointRadio), (float)keypointRadio * 2, (float)keypointRadio * 2);
                                }
                            }
                        }
                    }
                    bmp.Save(System.IO.Path.Combine(examDataSavePath, $"Ref_{fileName}.jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);

                }
            }

            File.WriteAllText(System.IO.Path.Combine(examDataSavePath, $"Ref_{fileName}.data"), JsonHelper.ToJson(this));
        }

    }


    public class TreeViewModel : BindableBase
    {
        protected bool _isChecked;
        public bool IsChecked
        {
            get => _isChecked; set
            {
                SetProperty(ref _isChecked, value);
                SetIsChecked(value, true, true);
            }
        }

        protected string _name;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        protected Visibility _visibility;
        public Visibility Visibility { get => _visibility; set => SetProperty(ref _visibility, value); }

        protected TreeViewModel _parent = null;
        public TreeViewModel Parent { get => _parent; set => SetProperty(ref _parent, value); }

        public ICollection<TreeViewModel> Children { get; set; } = new List<TreeViewModel>();


        private void SetIsChecked(bool value, bool updateChildren, bool updateParent)
        {
            if (updateChildren)
            {
                Children.ToList().ForEach(c => c.SetIsChecked(value, true, false));
            }
            if (updateParent && _parent != null)
            {
                _parent.VerifyCheckState();
            }
        }

        private void VerifyCheckState()
        {
            int count = Children.Where(x => x.IsChecked == false).Count();
            bool state = count > 0 ? false : true;
            IsChecked = state;
            SetIsChecked(state, false, true);
        }

    }
    
    public class AMenuItem : BindableBase
    {
        private string _glyph;
        public string Glyph { get => _glyph; set => SetProperty(ref _glyph, value); }

        private string _label;
        public string Label { get => _label; set => SetProperty(ref _label, value); }

        public string Code { get; set; }//区分点击的菜单
        public int Type { get; set; }//区分菜单还是目录

        private ICommand _command;
        public ICommand Command { get => _command; set => SetProperty(ref _command, value); }

        public ObservableCollection<AMenuItem> Children { get; set; } = new ObservableCollection<AMenuItem>();

        public void AddChildren(AMenuItem child)
        {
            this.Children.Add(child);
        }
    }


    //公司数据
    public class CompanyData : BindableBase
    {
        private string _name;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        public ObservableCollection<DempartmentData> DempartmentDatas { get; set; } = new ObservableCollection<DempartmentData>();
    }
    //部门数据
    public class DempartmentData : BindableBase
    {
        private string _name;
        public string Name { get => _name; set => SetProperty(ref _name, value); }

        public ObservableCollection<EmployeeData> EmployeeDatas { get; set; } = new ObservableCollection<EmployeeData>();
    }
    //员工
    public class EmployeeData : BindableBase
    {
        private string _name;
        public string Name { get => _name; set => SetProperty(ref _name, value); }
    }

}
